import { defineConfig, loadEnv } from 'vite'
import { resolve } from 'path'
import react from '@vitejs/plugin-react'
import WindiCSS from 'vite-plugin-windicss'

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  const VITE_VISIT_CONTEXT_PATH = loadEnv(mode, process.cwd())['VITE_VISIT_CONTEXT_PATH']
  const VITE_PROXY_URL = loadEnv(mode, process.cwd())['VITE_PROXY_URL']
  const VITE_BASE_URL = loadEnv(mode, process.cwd())['VITE_BASE_URL']

  console.log(command)

  return {
    base: VITE_VISIT_CONTEXT_PATH,
    server: {
      host: '0.0.0.0',
      proxy: {
        [VITE_BASE_URL]: {
          target: VITE_PROXY_URL,
          changeOrigin: true,
          rewrite: path => path.replace(/^\/api/, '')
        }
      }
    },
    resolve: {
      alias: {
        '@': resolve(__dirname, './src')
      },
      extensions: ['.tsx', '.ts', '.js', '.jsx', '.json', '.vue']
    },
    plugins: [
      react(),
      WindiCSS()
    ]
  }
})
